package br.edu.ufcg.greengrid.report;

import java.util.List;

import br.edu.ufcg.greengrid.model.Grid;
import br.edu.ufcg.greengrid.model.Simulator;
import br.edu.ufcg.greengrid.model.machine.Machine;
import br.edu.ufcg.greengrid.model.machine.state.MachineState.MSEnum;

import com.mallardsoft.tuple.Eleventuple;
import com.mallardsoft.tuple.Tuple;

public class MachineStateReporter extends
		AbstractReporter<Eleventuple<String, Long, Integer, Long, Integer, Long, Integer, Long, Integer, Integer, Long>> {

	// <machineId> <idle state time> <#idle->going-green> <going-green state time> <#going-green->green>
	// <green state time> <#green->leaving-green> <leaving-green state time> <#leaving-green->idle>
	// <#leaving-green->idle> <#idle->grid> <grid state time>

	public MachineStateReporter(long randomValue) {

		super(randomValue);
	}

	@Override
	public void cleanUp(Simulator s) {

		Grid grid = s.getGrid();
		List<Machine> machines = grid.getMachines();
		for (Machine m : machines) {
			add(Tuple.from(m.getId(), 
						   m.getTotalTimeSpentAtState(MSEnum.IDLE), 
						   m.getNumTransitions(MSEnum.IDLE, MSEnum.GOING_GREEN), 
						   m.getTotalTimeSpentAtState(MSEnum.GOING_GREEN), 
						   m.getNumTransitions(MSEnum.GOING_GREEN, MSEnum.GREEN), 
						   m.getTotalTimeSpentAtState(MSEnum.GREEN), 
						   m.getNumTransitions(MSEnum.GREEN, MSEnum.LEAVING_GREEN), 
						   m.getTotalTimeSpentAtState(MSEnum.LEAVING_GREEN), 
						   m.getNumTransitions(MSEnum.LEAVING_GREEN, MSEnum.IDLE), 
						   m.getNumTransitions(MSEnum.IDLE, MSEnum.GRID),
						   m.getTotalTimeSpentAtState(MSEnum.GRID)));

		}
	}

	@Override
	protected String getReportName() {

		return "machine-state";
	}

}
